package com.ruoyi.file.service.storage;

import com.ruoyi.file.domain.SysStorageConfig;
import com.ruoyi.file.enums.StorageTypeEnum;
import com.ruoyi.file.service.storage.impl.aliyun.AliyunOssStorageServiceImpl;
import com.ruoyi.file.service.storage.impl.aws.AwsS3StorageServiceImpl;
import com.ruoyi.file.service.storage.impl.local.LocalStorageServiceImpl;
import com.ruoyi.file.service.storage.impl.minio.MinioStorageServiceImpl;
import com.ruoyi.file.service.storage.impl.tencent.TencentCosStorageServiceImpl;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * 存储服务工厂
 * 根据存储类型返回相应的存储服务实现
 *
 * @author ruoyi
 */
@Component
public class StorageServiceFactory {

    private final LocalStorageServiceImpl localStorageService;
    private final MinioStorageServiceImpl minioStorageService;
    private final AliyunOssStorageServiceImpl aliyunOssStorageService;
    private final TencentCosStorageServiceImpl tencentCosStorageService;
    private final AwsS3StorageServiceImpl awsS3StorageService;

    public StorageServiceFactory(
            @Qualifier("localStorageService") LocalStorageServiceImpl localStorageService,
            @Qualifier("minioStorageService") MinioStorageServiceImpl minioStorageService,
            @Qualifier("aliyunOssStorageService") AliyunOssStorageServiceImpl aliyunOssStorageService,
            @Qualifier("tencentCosStorageService") TencentCosStorageServiceImpl tencentCosStorageService,
            @Qualifier("awsS3StorageService") AwsS3StorageServiceImpl awsS3StorageService) {
        this.localStorageService = localStorageService;
        this.minioStorageService = minioStorageService;
        this.aliyunOssStorageService = aliyunOssStorageService;
        this.tencentCosStorageService = tencentCosStorageService;
        this.awsS3StorageService = awsS3StorageService;
    }

    /**
     * 根据存储配置获取相应的存储服务实现
     *
     * @param config 存储配置
     * @return 存储服务实现
     */
    public StorageService getStorageService(SysStorageConfig config) {
        if (config == null || config.getStorageType() == null || config.getStorageType().isEmpty()) {
            // 默认使用本地文件存储
            return localStorageService;
        }
        
        try {
            StorageTypeEnum storageType = StorageTypeEnum.getByCode(config.getStorageType());
            return switch (storageType) {
                case LOCAL -> localStorageService;
                case MINIO -> minioStorageService;
                case ALIYUN -> aliyunOssStorageService;
                case TENCENT -> tencentCosStorageService;
                case AWS -> awsS3StorageService;
            };
        } catch (IllegalArgumentException e) {
            // 如果存储类型不支持，默认使用本地文件存储
            return localStorageService;
        }
    }

}
